package userinterface;

import java.awt.Dimension;
import java.text.DecimalFormat;
import java.util.List;

import javax.swing.table.AbstractTableModel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import alList.Grade;
import alList.Lesson;
import alList.StuGPA;
import alList.Student;

public class Preview extends BasicUI{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static Object[][] dataObjects;
	static int line;
	static int column;
	
	static class MyTableModel extends AbstractTableModel {

		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		@Override
		public int getRowCount() {
			// TODO Auto-generated method stub
			return line;
		}

		@Override
		public int getColumnCount() {
			// TODO Auto-generated method stub
			return column;
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			// TODO Auto-generated method stub
			return dataObjects[rowIndex][columnIndex];
		}
		
	}
	
	public static void previewGPA(List<StuGPA> dataList) {
		String[] colName = {"StuNum", "StuName", "StuGrade", "StuGPA", "StuCredit"};
		
		column = colName.length;
		line = dataList.size();
		dataObjects = new Object[line][column];

		MyTableModel myModel = new MyTableModel();
		JTable table = new JTable(myModel);		
		table.setPreferredScrollableViewportSize(new Dimension(500, 200));
		
		
		for (int j = 0; j < colName.length; j++) {
			table.getColumnModel().getColumn(j).setHeaderValue(colName[j]);
		}
		
		DecimalFormat decimalFormat = new DecimalFormat(".00");
		
		int i = 0;
		for (StuGPA dataLine : dataList) {
			dataObjects[i][0] = dataLine.getStuNum();
			dataObjects[i][1] = dataLine.getStuName();
			dataObjects[i][2] = decimalFormat.format(dataLine.getStuGrade());
			dataObjects[i][3] = decimalFormat.format(dataLine.getStuGPA());
			dataObjects[i][4] = dataLine.getStuCredit();
			i++;
		}

		JScrollPane scrollPane = new JScrollPane(table);
		container.add(scrollPane);
		scrollPane.setBounds(200, 285, 500, 200);
	}
	
	public static void previewStu(List<Student> dataList) {
		String[] colName = {"StuID", "StuName", "StuCla", "StuSpec", "StuDepart"};
		
		column = colName.length;
		line = dataList.size();
		dataObjects = new Object[line][column];

		MyTableModel myModel = new MyTableModel();
		JTable table = new JTable(myModel);		
		table.setPreferredScrollableViewportSize(new Dimension(500, 200));
		

		for (int j = 0; j < colName.length; j++) {
			table.getColumnModel().getColumn(j).setHeaderValue(colName[j]);
		}

		
		int i = 0;
		for (Student dataLine : dataList) {
			dataObjects[i][0] = dataLine.getNum();
			dataObjects[i][1] = dataLine.getName();
			dataObjects[i][2] = dataLine.getCla();
			dataObjects[i][3] = dataLine.getSpec();
			dataObjects[i][4] = dataLine.getDepart();
			i++;
		}

		JScrollPane scrollPane = new JScrollPane(table);
		container.add(scrollPane);
		scrollPane.setBounds(200, 285, 500, 200);
	}
	
	public static void previewCla(List<Lesson> dataList) {
		String[] colName = {"ClaNum", "ClaName", "Credit", "Teacher"};
		
		column = colName.length;
		line = dataList.size();
		dataObjects = new Object[line][column];
		
		MyTableModel myModel = new MyTableModel();
		JTable table = new JTable(myModel);		
		table.setPreferredScrollableViewportSize(new Dimension(500, 200));

		for (int j = 0; j < colName.length; j++) {
			table.getColumnModel().getColumn(j).setHeaderValue(colName[j]);
		}
		
		int i = 0;
		for (Lesson dataLine : dataList) {
			dataObjects[i][0] = dataLine.getNum();
			dataObjects[i][1] = dataLine.getName();
			dataObjects[i][2] = dataLine.getCredit();
			dataObjects[i][3] = dataLine.getTeach();
			i++;
		}

		
		JScrollPane scrollPane = new JScrollPane(table);
		container.add(scrollPane);
		scrollPane.setBounds(200, 265, 500, 200);
	}

	public static void previewGrade(List<Grade> dataList) {
		String[] colName = {"ClassNum", "StuNum", "Grade"};
		
		column = colName.length;
		line = dataList.size();
		dataObjects = new Object[line][column];
		
		MyTableModel myModel = new MyTableModel();
		JTable table = new JTable(myModel);		
		table.setPreferredScrollableViewportSize(new Dimension(500, 200));

		for (int j = 0; j < colName.length; j++) {
			table.getColumnModel().getColumn(j).setHeaderValue(colName[j]);
		}
		

		int i = 0;
		for (Grade dataLine : dataList) {
			dataObjects[i][0] = dataLine.getClaNum();
			dataObjects[i][1] = dataLine.getStuNum();
			if (dataLine.getMarkType() == 'S') {
				dataObjects[i][2] = dataLine.getScore();
			} else {
				dataObjects[i][2] = dataLine.getGrade();
			}
			i++;
		}

		JScrollPane scrollPane = new JScrollPane(table);
		container.add(scrollPane);
		scrollPane.setBounds(200, 265, 500, 200);
	}

}
